﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DAL;
using DotNet.Utilities;
using Entity;

namespace BLL
{
    public class UserBO
    {
        /// <summary>
        /// 获取今年最新的UserID;
        /// </summary>
        /// <param name="yearTail">年份的后两位</param>
        /// <returns></returns>
        public static string GetLatestUserIdByYearTail(string yearTail)
        {
            string result = string.Empty;
            string latestUserID = UserDAO.GetLatestUserIdByYearTail(yearTail);
            if (!string.IsNullOrWhiteSpace(latestUserID))
            {
                result = latestUserID;
            }
            return result;
        }

        /// <summary>
        /// 向数据库中插入User数据;
        /// </summary>
        /// <param name="user"></param>
        public static int InsertUser(User user)
        {
            return UserDAO.InsertUser(user);
        }

        /// <summary>
        /// 通过UserName获得相应的User对象;
        /// </summary>
        /// <param name="userName"></param>
        public static User GetUserByUserName(string userName)
        {
            User user = new User();

            DataTable dt = UserDAO.GetUserTableByUserName(userName);
            if (dt != null && dt.Rows.Count > 0)
            {
                user.Id = (int)dt.Rows[0]["Id"];
                user.UserId = dt.Rows[0]["UserId"].ToString();
                user.UserName = dt.Rows[0]["UserName"].ToString();
                user.Password = dt.Rows[0]["Password"].ToString();
                user.Email = dt.Rows[0]["Email"].ToString();
                user.Type = dt.Rows[0]["Type"].ToString();
                user.Telephone = dt.Rows[0]["Telephone"].ToString();
                user.Cellphone = dt.Rows[0]["Cellphone"].ToString();
                user.Regdate = Convert.ToDateTime(dt.Rows[0]["Regdate"]);
            }

            return user;
        }

        /// <summary>
        /// 通过UserID获得User.Id; 未找到返回-1;
        /// </summary>
        /// <param name="userId">string User.UserId</param>
        /// <returns>int User.Id; </returns>
        public static int GetIdByUserId(string userId)
        {
            return UserDAO.GetIdByUserId(userId);
        }

        /// <summary>
        /// 通过Id获得User对象;
        /// </summary>     
        public static User GetUserById(int id)
        {
            User user = new User();

            DataTable dt = UserDAO.GetUserById(id);
            if (dt != null && dt.Rows.Count > 0)
            {
                user.Id = (int)dt.Rows[0]["Id"];
                user.UserId = dt.Rows[0]["UserId"].ToString();
                user.UserName = dt.Rows[0]["UserName"].ToString();
                user.Password = dt.Rows[0]["Password"].ToString();
                user.Email = dt.Rows[0]["Email"].ToString();
                user.Type = dt.Rows[0]["Type"].ToString();
                user.Telephone = dt.Rows[0]["Telephone"].ToString();
                user.Cellphone = dt.Rows[0]["Cellphone"].ToString();
                user.Regdate = Convert.ToDateTime(dt.Rows[0]["Regdate"]);
            }

            return user;
        }

        /// <summary>
        /// 通过Id获得UserName;
        /// </summary>      
        /// <returns>可能会返回null</returns>
        public static string GetUserNameById(int id)
        {
            User user = GetUserById(id);
            return user.UserName;
        }

        public static string GetUserNameAndUserId(int id)
        {
            User user = GetUserById(id);
            string nameAndUserId = user.UserName + StringHelper.SurroundStr(user.UserId, "（）");
            return nameAndUserId;
        }
    }
}
